import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import store from '../store'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/',
        component: () => import('../components/MainPage/HomePage')
      },
      {
        path: '/courses',
        component: () => import('../components/MainPage/CoursePage')
      },
      {
        path: '/courses/:id',
        component: () => import('../components/MainPage/CoursePage'),
        props: true
      },
      {
        path: '/channel/:uid',
        component: () => import('../components/MainPage/CurrentCoursePage'),
        props: true
      },
      {
        path: '/channel/:uid/:pid',
        component: () => import('../components/MainPage/CurrentCoursePage'),
        props: true
      }
    ]
  },
  {
    path: '/classroom',
    name: 'Classroom',
    component: () => import('../components/MainPage/ClassPage')
  },
  {
    path: '/group',
    name: 'Group',
    component: () => import('../components/MainPage/GroupPage')
  },
  {
    path: '/group/search_group',
    component: () => import('../components/MainPage/SearchGroup'),
    name: 'SearchGroup'
  },
  {
    path: '/me',
    name: 'Me',
    component: () => import('../components/MainPage/LearnMainPage')
  },
  {
    path: '/login',
    component: () => import('../components/MainPage/LoginPage')
  },
  {
    path: '/register',
    component: () => import('../components/MainPage/RegisterPage')
  },
  {
    path: '/middleshow',
    name: 'Middleshow',
    component: () => import('../components/MainPage/MiddleShow')
  },
  {
    path: '/settings',
    name: 'Setting',
    component: () => import('../components/MainPage/SettingPage')
  }
]

const router = createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name === 'Me' && !store.state.token) {
    next({ name: 'Middleshow' })
  } else {
    next()
  }
})

export default router
